﻿using CMS6Tools.Common;
using CMS6Tools.Performances.DataAnalytics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace CMS6Tools.Management
{
    public partial class CMSHostingForm : DockContent
    {
        public CMSHostingForm()
        {
            InitializeComponent();
        }

        private void GetProjectsButton_Click(object sender, EventArgs e)
        {
            var prtgApiClient = GetPrtgApiClient();

            var getSensorData = prtgApiClient.GetSensorData();

            var cms6SiteSensors = getSensorData.Sensors
                                               .Where(c => c.Device != null
                                                        && c.Device.StartsWith("CMS6", StringComparison.InvariantCultureIgnoreCase)
                                                        && !c.Device.Equals("cms6-vmc", StringComparison.InvariantCultureIgnoreCase)
                                                        && c.Group != null
                                                        && c.Group.Equals("WEB SITES STATUS")
                                                        && c.Type.StartsWith("HTTP", StringComparison.InvariantCultureIgnoreCase))
                                               .ToList();
                                               //.Take(10);
            foreach (var item in cms6SiteSensors)
            {
                item.URL = prtgApiClient.GetSensorURL(item.ID);
            }

            var projectDataSource = cms6SiteSensors.Select(a => new ProjectModel
            {
                ID = a.ID,
                Name = a.Name,
                URL = a.URL,
                Device = a.Device,
                Status = a.Status
            }).OrderBy(a => a.Name).ToList(); 

            projectsDataGridView.AutoGenerateColumns = false;
            projectsDataGridView.DataSource = projectDataSource;
        }

        PrtgApiClient GetPrtgApiClient()
        {
            return new PrtgApiClient(PRTGUrlTextBox.Text, UserNameTextBox.Text, PasswordTextBox.Text);
        }

        private void CMSHostingForm_Load(object sender, EventArgs e)
        { 
        }

        private ProjectModel AssignSiteVersion(ProjectModel model)
        {
            if (string.IsNullOrEmpty(model.URL))
                return model;

            try
            {
                var statusUrl = string.Format("{0}/version.vinfo", model.URL.TrimEnd('/'));
                var responseString = WebUtils.DoGet(statusUrl);
                var regex = new Regex(@"<h2>([^<>\d]*)([\d.]*)<\/h2>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var matches = regex.Matches(responseString);
                if (matches.Count == 2)
                {
                    model.DBStructureVersion = matches[0].Groups[2].Value;
                    model.DBDataVersion = matches[1].Groups[2].Value;
                }
                var siteRegex = new Regex(@"<h2>([\/\d]*)\(([\d.]*)\)<\/h2>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var siteMatches = siteRegex.Matches(responseString);
                if (matches.Count > 0)
                {
                    model.SiteVersion = matches[0].Groups[2].Value;
                }
            }
            catch
            { 
            }
            return model;
        }

        private void CheckVersionButton_Click(object sender, EventArgs e)
        {
            var datasource = projectsDataGridView.DataSource as IEnumerable<ProjectModel>;
            foreach (var item in datasource)
            {
                AssignSiteVersion(item);
            }
            projectsDataGridView.DataSource = datasource.OrderBy(a=>a.Name).ToList();
        }
    }

    public class ProjectModel
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string URL { get; set; }
        public string Device { get; set; }
        public string Status { get; set; }
        public string SiteVersion { get; set; }
        public string DBStructureVersion { get; set; }
        public string DBDataVersion { get; set; }
    }
}
